package com.tongcao.cn.executor;

import com.tongcao.cn.base.req.IRequest;
import com.tongcao.cn.dfire.result.Result;
import com.tongcao.cn.dfire.utils.ResultUtil;
import org.springframework.util.StopWatch;

/**
 * @Author:tongcao
 * @Date:2019/1/24 16:25
 * Description:
 */
public abstract class AbstractInnerExecutor<RST extends IRequest, RSP> implements IInnerExecutor<RST, Result<RSP>> {


    /**
     * 执行方法
     *
     * @param request
     * @return
     */
    @Override
    public Result<RSP> execute(RST request) {
        StopWatch stopWatch = new StopWatch();
        stopWatch.start();

        Result<RSP> result;
        try {
            result = _execute(request);
        } catch (Exception e) {
            result = ResultUtil.failResult("400", "异常啦");
        } catch (Throwable throwable) {
            result = ResultUtil.failResult("400", "异常啦");
            stopWatch.stop();
        }
        return result;
    }

    /**
     * 是否可用
     *
     * @return
     */
    @Override
    public boolean isDisable() {
        return false;
    }

    /**
     * 执行优先级
     *
     * @return
     */
    @Override
    public int order() {
        return 0;
    }

    /**
     * 子类待实现的方法
     *
     * @param request
     * @return
     * @throws Exception
     */
    protected abstract Result<RSP> _execute(RST request) throws Exception;

}
